/**
 * 弹出层 dialog
 * @author zelipe
 */
define(['bootstrap/bootstrap-modal'], function () {
    var obj = {};

    var uuid = T.uuid();

    var dialogId = '__modal-dialog';
    var dialogEle = function (conf) {
        $('#' + dialogId).modal('hide');
        $('#' + dialogId).remove();
        var width = 720;
        var height = 390;
        var _type = conf.type || 'normal', _isIframe = _type == 'iframe', _isUrl = _type == 'url';
        var _getContent = function () {
            var _html = '', _confContent = conf.content;
            if (_isIframe) {
                if (conf.width)
                    width = conf.width;
                if (conf.height)
                    height = conf.height;
                _html = '<iframe style="width:' + width + 'px;height:' + height + 'px;border:none;margin:0;padding:0" border="0" frameborder="0" src="' + _confContent + '"></iframe>';
            } else if (_isUrl) {
                // html 生成后处理url load
            } else {
                _html = _confContent;
            }
            return _html;
        }

        var _html = '';
        if (conf.title)
            _html += '<div class="modal-header"><strong style="font-weight: bold;font-size: 14px;">' + conf.title + '</strong><span title="关闭" style="position:absolute;right:5px;top:3px;width:21px;height:21px;overflow:hidden;text-indent:-100em;cursor:pointer;background:url(/webos/images/appimg/widgets_close.png);" data-dismiss="modal">关闭</span></div>';

        _html += '<div class="modal-body" id="__modalContent">' + _getContent() + '</div>';

        if (conf.ok != false || conf.cancel != false) {
            _html += '<div class="modal-footer">';

            if (conf.ok) {
                _html += '<a href="javascript:;" id="btnOk' + uuid + '" class="btn btn-primary" >确定</a>';
            }
            if (conf.cancel)
                _html += '<a href="javascript:;" class="btn" id="btnCancel' + uuid + '" data-dismiss="modal">取消</a>';

            _html += '</div>';
        }
        // size
        // modal-mini, modal-small, modal-medium, modal-large, modal-xlarge, modal-xxlarge, modal-max
        var _size = conf.size || 'mini';
        _size = ' modal-' + _size;
        var attr;
        if (conf.type == 'iframe'){
            var _width = width +10;
            attr = {'id':dialogId, 'class':'modal fade' + _size, 'style':'top:40%;left:30%;width:'+_width+'px;margin:-200px 0 0 -150px;'}
        }
        else if(conf.type =='url')
			attr = {'id':dialogId, 'class':'modal fade' + _size};           
		else
			attr = {'id':dialogId, 'class':'modal fade' + _size, 'style':'width:360px;'};
        var e = $('<div/>').attr(attr).html(_html || '');


        $('body').append(e);
        if (_isIframe)
            $('#__modalContent').css({padding:5})
        else if (_isUrl)
            INIT._loadPage('__modalContent', conf.content);
        $('#' + dialogId).modal({
            backdrop:'static'
        });
        if (conf.onHidden) {
            $('#' + dialogId).on('hidden', function () {
                if (typeof(conf.onHidden) == 'function')
                    conf.onHidden();
            });
        }

        $('#' + dialogId).modal('show');
        $('#' + dialogId).delegate('#btnOk' + uuid, 'click', function () {
            if (typeof(conf.ok) == 'function')
                conf.ok();
            if (conf.preventClose) {

            } else
                $('#' + dialogId).modal('hide');
        });
    }

    obj.alert = function (conf) {
        conf.cancel = false;
        if (conf.ok == undefined)
            conf.ok = true;
        conf.ok = (conf.ok) ? conf.ok : false;
        dialogEle(conf);
    };

    obj.confirm = function (conf) {

        conf.cancel = (conf.cancel == undefined || conf.cancel) ? true : false;
        if (conf.ok == undefined)
            conf.ok = true;
        conf.ok = (conf.ok) ? conf.ok : false;
        dialogEle(conf);
    };

    return obj;
});